using System.IO;
using EnvDTE;
using EnvDTE80;
using Microsoft.Win32;

namespace VSSDKAssist.Recipes.Common.Helpers
{
    /// <summary>
    /// Common Helper class
    /// </summary>
    public static class CommonHelper
    {
        /// <summary>
        /// Gets the random number.
        /// </summary>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public static int GetRandomNumber()
        {
            //Sleep to obtain a really random number because of the cpu ticks
            System.Threading.Thread.Sleep(100);
            return new System.Random().Next(System.UInt16.MinValue, System.UInt16.MaxValue);
        }

        /// <summary>
        /// Gets the vs SDK install path.
        /// </summary>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public static string GetVSSdkInstallPath()
        {
            using(RegistryKey key = Registry.LocalMachine.OpenSubKey(Constants.VSSdkInstallDirPathKey))
            {
                if(null == key)
                    return null;

                return (string)key.GetValue(Constants.VSSdkInstallDirKeyValue);
            }
        }

        /// <summary>
        /// Gets the vs install path.
        /// </summary>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public static string GetVSInstallPath()
        {
            using(RegistryKey key = Registry.LocalMachine.OpenSubKey(Constants.VSInstallDirPathKey))
            {
                if(null == key)
                    return null;

                return (string)key.GetValue(Constants.VSInstallDirKeyValue);
            }
        }

        /// <summary>
        /// Gets the vs install path.
        /// </summary>
        /// <returns></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
        public static string GetWindowsSDKBinPath()
        {
            using(RegistryKey key = Registry.LocalMachine.OpenSubKey(Constants.WindowsSDKPathKey))
            {
                if(null == key)
                    return null;

                string sdkInstallationFolder = (string)key.GetValue(Constants.WindowsSDKInstallationFolderKeyValue);

                return Path.Combine(sdkInstallationFolder, "bin");
            }
        }

        public static string GetTTRelativePath(CodeClass codeClass)
        {
            if(codeClass.Language.Equals(CodeModelLanguageConstants.vsCMLanguageCSharp))
            {
                return @"TT\cs";
            }
            else if(codeClass.Language.Equals(CodeModelLanguageConstants.vsCMLanguageVB))
            {
                return @"TT\vb";
            }
            else if(codeClass.Language.Equals(CodeModelLanguageConstants2.vsCMLanguageJSharp))
            {
                return @"TT\js";
            }
            else
            {
                return @"TT\cs";
            }
        }

        public static string GetSelectedProjectLanguage(DTE dte)
        {
            if(dte.SelectedItems.Count == 1)
            {
                Project project = dte.SelectedItems.Item(1).Project as Project;

                if(project != null && project.CodeModel != null)
                {
                    if(project.CodeModel.Language.Equals(CodeModelLanguageConstants.vsCMLanguageCSharp))
                    {
                        return "cs";
                    }
                    else if(project.CodeModel.Language.Equals(CodeModelLanguageConstants.vsCMLanguageVB))
                    {
                        return "vb";
                    }
                    else if(project.CodeModel.Language.Equals(CodeModelLanguageConstants2.vsCMLanguageJSharp))
                    {
                        return "js";
                    }
                    else
                    {
                        return "cs";
                    }
                }
            }

            return string.Empty;
        }
    }
}